import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainScreen 
	extends JPanel
	implements Runnable{
	
	Image playerImage, stoneImage, enermyImage, buletImage, missileImage;
	
	Player player;
	
	long prevStoneCreationTime = 0;//객체형 변수인 경우 0으로 자동 초기화
	final int stoneCreationSpeed = 200;//1초
	
	LinkedList<Enermy4> enermy4 = new LinkedList<Enermy4>();
	LinkedList<Stone> stones = new LinkedList<Stone>();
	
	public MainScreen () {
		try {
		URL ur1 = getClass().getResource("stone.png");
			Image image = ImageIO.read(ur1);
			stoneImage = image.getScaledInstance(Constants.SCREEN_WIDTH, Constants.SCREEN_HEGHT, image.SCALE_SMOOTH);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setBackground(Color.black);
		
		Thread thread = new Thread(this);
		thread.start();
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		// TODO Auto-generated method stub
		super.paintComponent(g);
		for (Stone stone : stones){
			stone.render(g);
		}
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			for (int i = stones.size() - 1; i >= 0 ; i--) {
				Stone stone = stones.get(i);
				stone.update();
				if (stone.active == false) {
					stones.remove(stone);
				}
			}

			
		
		//0.2초 마다 한 번씩 Stone 생성 
		//1970.2.1 0:0:0 -> 60*60*24*365*1000
		long currentTime = System.currentTimeMillis();//현재 시간
		if (currentTime - prevStoneCreationTime > stoneCreationSpeed) {
			Stone stone = new Stone(stoneImage);
			stone.active = true;
			stones.add(stone);
			prevStoneCreationTime = currentTime;
		}
		
	
		
		//화면 출력
		repaint();
		
		try {
			Thread.sleep(30);
		} catch (Exception ex) {
			
		}
	}
	
	}

}
